package cn.edu.zut.hhr.config;

import cn.edu.zut.hhr.interceptor.LoginInterceptor;
import cn.edu.zut.hhr.interceptor.UserInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

@Configuration
public class WebMVCConfig implements WebMvcConfigurer {

    @Resource
    LoginInterceptor loginInterceptor;
    @Resource
    UserInterceptor userInterceptor;

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                .allowedMethods("GET","POST","DELETE","PUT")
                .maxAge(3600)
                .allowCredentials(true);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        registry.addInterceptor(loginInterceptor)
                /**
                 * 暂时还没有需要拦截的
                 */
//                .addPathPatterns("/**")
                .addPathPatterns("/comment/create/**","/article/create/**","/article/update/**","/article/save/**")
                .excludePathPatterns("/static/**","/login")
                .order(1);

        registry.addInterceptor(userInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns("/static/**","/login")
                .order(0);
    }
}
